#include "HelpWindow.h"

QString HelpWindow::INDEX_PAGE = "";
QString HelpWindow::Q_ICON = "";
QString HelpWindow::WINDOW_TITLE = "";

HelpWindow *HelpWindow::getInstance(const QString &helpFile, QWidget *parent) {
    static QMutex mutex;
    static HelpWindow* instance = nullptr;
    if (!instance) {
        if (!QFileInfo::exists(helpFile)) {
            QMessageBox::warning(nullptr, "错误", "找不到帮助文档文件");
            return nullptr;
        }

        QMutexLocker locker(&mutex);
        if (!instance) {
            instance = new HelpWindow(helpFile, parent);
            // 设置窗口属性
            instance->setAttribute(Qt::WA_DeleteOnClose, false);
        }
    }
    return instance;
}

HelpWindow::HelpWindow(const QString &helpFile, QWidget *parent)
    : QDialog(parent)
{
    setWindowIcon(QIcon(Q_ICON));
    setWindowTitle(WINDOW_TITLE);
    resize(800, 600);
    // 创建帮助引擎
    m_helpEngine = new QHelpEngine(helpFile, this);
    if (!m_helpEngine->setupData()) {
        QMessageBox::warning(this, "错误", "无法初始化帮助系统");
        return;
    }

    // 创建界面
    QTabWidget *tabs = new QTabWidget(this);
    QSplitter *splitter = new QSplitter(Qt::Horizontal, this);

    // 内容目录
     contentWidget = m_helpEngine->contentWidget();
    contentWidget->setRootIsDecorated(true);
    contentWidget->expandAll();
    splitter->addWidget(contentWidget);

    // 帮助内容浏览器
    m_textBrowser = new HelpBrowser(m_helpEngine, this);
    m_textBrowser->setSource(QUrl(INDEX_PAGE));
    splitter->addWidget(m_textBrowser);

    splitter->setStretchFactor(1, 1);
    tabs->addTab(splitter, "目录");

    // 索引
    QHelpIndexWidget *indexWidget = m_helpEngine->indexWidget();
    QSplitter *indexSplitter = new QSplitter(Qt::Horizontal);
    indexSplitter->addWidget(indexWidget);

    HelpBrowser *indexBrowser = new HelpBrowser(m_helpEngine);
    indexSplitter->addWidget(indexBrowser);
    indexSplitter->setStretchFactor(1, 1);

    tabs->addTab(indexSplitter, "索引");

    // 连接信号
    connect(contentWidget,
            SIGNAL(linkActivated(QUrl)),
            m_textBrowser, SLOT(setSource(QUrl)));

    connect(indexWidget,
            SIGNAL(linkActivated(QUrl, QString)),
            indexBrowser, SLOT(setSource(QUrl)));

    // 获取内容模型（不是QHelpContentWidget！）
    QHelpContentModel *contentModel = m_helpEngine->contentModel();
    // 正确连接信号
    connect(contentModel, &QHelpContentModel::contentsCreated, [=]() {
        m_helpEngine->contentWidget()->expandAll(); // 此时内容已加载
        qDebug() << "Expand all executed";
    });


    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(tabs);
}

void HelpWindow::showHelpPage(const QString &page)
{
    m_textBrowser->setSource(QUrl(page));
}

void HelpWindow::showHelp(const QString &page) {
    m_textBrowser->setSource(QUrl(page));
    show();
    raise();
    activateWindow();
}

void HelpWindow::closeEvent(QCloseEvent *event) {
    hide(); // 隐藏而不是关闭
    event->ignore(); // 忽略关闭事件
    qInfo() << "close HelpWindow, not delete HelpWindow, just hide it";
}
